from .server import Server
import json


class ProxyForRobot(object):
    """
    set instance work for global
    """
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

    def __init__(self):
        """BrowserMobProxyLibrary is a Robot Framework library ro interface with BrowserMob Proxy.
        """
        self.server = None
        self.client = None

    def start_local_server(self, path='browsermob-proxy', port=25324):
        """Start local Browsermob Proxy Server instance

        :param path: Path to the browsermob proxy batch file
        :param options: Dictionary that can hold the port. \
        """
        port = {'port': int(port)}
        self.server = Server(path=path, options=port)
        self.server.start()

    def stop_local_server(self):
        """This will stop the process running the Browsermob Proxy
        """
        if isinstance(self.server, Server):
            self.server.stop()
    
    def create_proxy(self, params={'port': 25320}):
        """Creates proxy.
        :param params: Dictionary where you can specify params \
                    like httpProxy and httpsProxy
        """

        # self.client = self.server.create_proxy(params={"port": 8083})
        self.client = self.server.create_proxy(params)
        return self.client.proxy
    
    def close_proxy(self):
        """shuts down the proxy and closes the port
        """

        return self.client.close()
    
    def new_har(self, ref=None, options={}):
        """This sets a new HAR to be recorded
        :param ref: A reference for the HAR. Defaults to None
        :param options: A dictionary that will be passed to BrowserMob Proxy \
                   with specific keywords. Keywords are: \
                   captureHeaders - Boolean, capture headers \
                   captureContent - Boolean, capture content bodies \
                   captureBinaryContent - Boolean, capture binary content
        """

        return self.client.new_har(ref=ref, options=options)
    
    def get_har(self):
        """Returns the HAR that has been recorded
        """

        return self.client.har

    def get_har_as_json(self):
        """Returns the HAR that has been recorded as json
        """

        return json.dumps(self.client.har)
    
    def stop_all_server_client(self):
        print("stop_all_server_client start")
        self.server.stop()
        print("stoped server")
        self.client.close()
        print("stop_all_server_client enf")
    
    def add_header(self, headers_list):
        print('headers_list: ', headers_list)
        headers = {}
        for i in range(0, len(headers_list), 2):
            headers[headers_list[i]]=headers_list[i+1]
        print('headers dict: ', headers)
        self.client.headers(headers)

        
    def add_header_by_path(self, header_path):
        f = open(header_path)
        print('add_header_by_path f: ', f)
        headers = json.load(f)
        print('add_header_by_path headers: ', headers)
        self.client.headers(headers)
        f.close()
    

        
